//
//  BYSelectionDetails.m
//  FangXinLang
//
//  Created by AJY on 16/9/18.
//  Copyright © 2016年 AJY. All rights reserved.
//

#import "BYSelectionDetails.h"
#import "BYSelectionView.h"
#import <AFNetworking.h>
#import "TitleModel.h"
#define view_addWith 5
#define view_width ([UIScreen mainScreen].bounds.size.width-20*5)/4
@implementation BYSelectionDetails

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self addData];
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

-(void)addData{
    AFHTTPSessionManager *manager=[AFHTTPSessionManager manager];
    [manager GET:@"http://newsapi.sina.cn/?resource=nav&modifytime=0&deviceId=cf3a669aaccc85e5&from=6053295012&weiboUid=&weiboSuid=&imei=861717030685322&wm=b207&chwm=14020_0001&oldChwm=14020_0001&osVersion=5.1&connectionType=2&resolution=720x1184&city=CHXX0122&deviceModel=HUAWEI__HUAWEI__HUAWEI+TIT-AL00&location=0.0%2C0.0&link=&mac=bc%3A75%3A74%3Aab%3Abb%3A77&ua=HUAWEI-HUAWEI+TIT-AL00__sinanews__5.3.2__android__5.1&urlSign=55e528852e&rand=989" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        self.arrtitle=[NSMutableArray array];
        self.arrname=[NSMutableArray array];
        self.arrurl=[NSMutableArray array];
        //[responseObject writeToFile:@"/Users/ajy/Desktop/xinlang.plist" atomically:YES];
        //获取数据
        NSArray *list=responseObject[@"data"][@"news"][@"headlines"][@"list"];
        for (NSDictionary *dic in list) {
            TitleModel *model=[TitleModel titlemodelWith:dic];
            [self.arrtitle addObject:model];
            [self.arrname addObject:dic[@"name"]];
            [self.arrurl addObject:dic[@"id"]];
        }
        if(list !=nil)
        {
            [self makeMainContent];
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
}

-(NSMutableArray *)views1
{
    if (_views1 == nil) {
        _views1 = [NSMutableArray array];
    }
    return _views1;
}

-(NSMutableArray *)views2
{
    if (_views2 == nil) {
        _views2 = [NSMutableArray array];
    }
    return _views2;
}

-(void)makeMainContent
{
    
    
    NSMutableArray *listArray = @[@"头条",@"娱乐",@"视频",@"推荐",@"体育"];
    
    NSArray *otherValues =self.arrname;
    //点击添加频道的label和他的背景色
    UIView *bg_view = [[UIView alloc] initWithFrame:CGRectMake(0,20+45*((listArray.count -1)/4+1),[UIScreen mainScreen].bounds.size.width, 30)];
    bg_view.backgroundColor = [UIColor whiteColor];
    UILabel *morevalue_label = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, 100, 30)];
    morevalue_label.text = @"点击添加频道";
    morevalue_label.font = [UIFont systemFontOfSize:14];
    [bg_view addSubview:morevalue_label];
    [self addSubview:bg_view];
    
    NSInteger num = listArray.count;
    for (int i =0; i <num; i++) {
        BYSelectionView *view = [[BYSelectionView alloc] init];
        view.frame = CGRectMake(20+(20+view_width)*(i%4), 20+45*(i/4), view_width, 25);
        [view makeSelectionViewWithTitle:listArray[i]];
        [self.views1 addObject:view];
        view.tag = 1;
        view->views_array = self.views1;
        view->views1 = self.views1;
        view->views2 = self.views2;
        [view setMoreChanelslabel:bg_view];
        [self addSubview:view];
    }
    
    NSInteger num2 = otherValues.count;
    for (int i=0; i<num2; i++) {
        BYSelectionView *view = [[BYSelectionView alloc] initWithFrame:CGRectMake(20+(20+view_width)*(i%4),CGRectGetMaxY(bg_view.frame)+20+45*(i/4), view_width, 25)];
        [view makeSelectionViewWithTitle:otherValues[i]];
        [view setMoreChanelslabel:bg_view];
        [self.views2 addObject:view];
        view.tag = 0;
        view->views_array = self.views2;
        view->views1 = self.views1;
        view->views2 = self.views2;
        [self addSubview:view];
    }
    
    self.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, CGRectGetMaxY(bg_view.frame)+20+45*((num2-1)/4+1) + 50);
    
}
@end
